import pandas as pd
from futu import *

from tqdm import tqdm
pbar = tqdm()

quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)  # 创建行情对象

for code in open("alist.txt").read().split():
    dataset = []
    ret, data, page_req_key = quote_ctx.request_history_kline(code, start='1900-01-01', end='2021-3-19')
    dataset.append(data)
    pbar.update()

    while page_req_key != None:
        ret, data, page_req_key = quote_ctx.request_history_kline(code, start='1900-01-01', end='2021-3-19', page_req_key=page_req_key)
        dataset.append(data)
        pbar.update()
    pd.concat(dataset).to_csv(f"{code}.csv", index=False)

quote_ctx.close()
